/**
 * 一行文本
 */
export class Line {
    /**
     * 文本内容
     */
    readonly content: string;

    /**
     * 字符串的起始位置
     */
    readonly #start: number;

    /**
     * 字符串的结束位置
     */
    readonly #end: number;

    /**
     * 当前的游标位置
     */
    #cursor: number;

    constructor(content: string, start: number, end: number) {
        this.content = content;
        this.#start = start;
        this.#end = end;
        this.#cursor = start;
    }

    /**
     * 获取所有的 char code
     */
    *charCodes(): Generator<number> {
        while (this.#cursor < this.#end) {
            yield this.content.charCodeAt(this.#cursor++);
        }
    }

    /**
     * 获取当前行的缩进，并将游标移动到第一个非空白字符
     */
    getIndent(): number {
        for (let i = 0; i < this.#end; i++) {
            if (this.content[this.#cursor + i] !== " ") {
                this.#cursor = this.#cursor + i;
                return i;
            }
        }
        const len = this.#end - this.#cursor;
        this.#cursor = this.#end;
        return len;
    }

    /**
     * 获取 cursor 直到结束的字符串
     */
    getRest(): string {
        const str = this.content.slice(this.#cursor, this.#end);
        this.#cursor = this.#end;
        return str;
    }

    #getAttributeValue(): string {
        let quote = " ";
        if (this.content[this.#cursor] === '"' || this.content[this.#cursor] === "'") {
            quote = this.content[this.#cursor]!;
            this.#cursor += 1;
        }
        const start = this.#cursor;
        while (this.#cursor < this.#end) {
            if (this.content[this.#cursor] === quote) {
                this.#cursor += 1;
                console.log(this.content.slice(start, this.#cursor - 1));
                return this.content.slice(start, this.#cursor - 1);
            }
            this.#cursor += 1;
        }
        return this.content.slice(start, this.#cursor);
    }

    /**
     * 获取一个属性
     */
    getAttribute(): [string, string] {
        let start = this.#cursor;
        let name = "";
        let value = "";

        while (this.#cursor < this.#end) {
            switch (this.content[this.#cursor]) {
                case " ": {
                    this.#cursor += 1;
                    name = this.content.slice(start, this.#cursor - 1);
                    return [name, value];
                }
                case "=": {
                    name = this.content.slice(start, this.#cursor);
                    this.#cursor += 1;
                    value = this.#getAttributeValue();
                    return [name, value];
                }
            }
            this.#cursor += 1;
        }
        name = this.content.slice(start, this.#cursor);
        return [name, value];
    }

    /**
     * 获取一个 css class name
     */
    getClassName(): string {
        let start = this.#cursor;
        while (this.#cursor < this.#end) {
            if (this.content[this.#cursor] === " ") {
                break;
            }
            this.#cursor += 1;
        }
        return this.content.slice(start, this.#cursor);
    }

    /**
     * 获取一个 tag name
     */
    getTagName(): string {
        let start = this.#cursor;
        while (this.#cursor < this.#end) {
            if (this.content[this.#cursor] === " ") {
                break;
            }
            this.#cursor += 1;
        }
        return this.content.slice(start, this.#cursor);
    }

    toString(): string {
        return this.content.slice(this.#start, this.#end);
    }

    isEmptyOrSpace(): boolean {
        for (let i = this.#start; i < this.#end; i++) {
            if (this.content[i] !== " ") {
                return false;
            }
        }
        return true;
    }
}

export function* splitLine(str: string): Generator<Line> {
    let start = 0;
    for (let i = 0; i < str.length; i++) {
        if (str[i] === "\n") {
            yield new Line(str, start, i);
            start = i + 1;
        }
    }
    if (start < str.length) {
        yield new Line(str, start, str.length);
    }
}
